package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;


    /**
     * 上传本地音频文件到腾讯云点播平台
     *
     * @param file 声音文件
     * @return 上传成功后点播平台：音频文件唯一标识、音频文件播放地址
     */
    @Operation(summary = "上传本地音频文件到腾讯云点播平台")
    @PostMapping("/trackInfo/uploadTrack")
    public Result<Map<String, String>> uploadTrack(MultipartFile file) {
        Map<String, String> mapResult = vodService.uploadTrack(file);
        return Result.ok(mapResult);
    }


    /**
     * 该接口必须才能访问
     * 内容创作者、运营人员保存声音
     *
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "保存声音")
    @PostMapping("/trackInfo/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
        //1.先获取登录用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.调用业务层保存声音
        trackInfoService.saveTrackInfo(userId, trackInfoVo);
        return Result.ok();
    }


    /**
     * 该接口必须登录才可以访问
     * 根据条件分页查询当前登录用户声音列表
     *
     * @param page
     * @param limit
     * @param trackInfoQuery
     * @return
     */
    @GuiGuLogin
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    @Operation(summary = "根据条件分页查询当前登录用户声音列表")
    public Result<Page<TrackListVo>> getUserTrackPage(@PathVariable int page, @PathVariable int limit, @RequestBody TrackInfoQuery trackInfoQuery) {
        //1.获取登录用户ID 封装到查询对象中
        trackInfoQuery.setUserId(AuthContextHolder.getUserId());
        //2.构建分页对象
        Page<TrackListVo> pageInfo = new Page<>(page, limit);
        //3.调用业务层查询数据，响应分页对象
        pageInfo = trackInfoService.getUserTrackPage(pageInfo, trackInfoQuery);
        return Result.ok(pageInfo);
    }


    /**
     * 根据声音ID查询声音详情
     *
     * @param id
     * @return
     */
    @Operation(summary = "根据声音ID查询声音详情")
    @GetMapping("/trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }


    /**
     * 修改声音
     *
     * @param id
     * @return
     */
    @Operation(summary = "修改声音信息")
    @PutMapping("/trackInfo/updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable Long id, @RequestBody @Validated TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(id, trackInfoVo);
        return Result.ok();
    }

    /**
     * 该接口需要登录才能访问
     * 删除声音
     *
     * @param id
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "根据声音ID删除声音")
    @DeleteMapping("/trackInfo/removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id) {
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }


    /**
     * 用于小程序端专辑页面展示分页声音列表，动态根据用户展示声音付费标识
     *
     * @param albumId
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin(required = false)
    @Operation(summary = "用于小程序端专辑页面展示分页声音列表，动态根据用户展示声音付费标识")
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> getAlbumTrackPage(@PathVariable Long albumId, @PathVariable Integer page, @PathVariable Integer limit) {
        //1.获取用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.封装分页对象
        Page<AlbumTrackListVo> pageInfo = new Page<>(page, limit);
        //3.调用业务层封装分页对象
        pageInfo = trackInfoService.getAlbumTrackPage(pageInfo, albumId, userId);
        return Result.ok(pageInfo);
    }


    /**
     * 根据声音ID查询声音统计信息
     *
     * @param trackId
     * @return
     */
    @Operation(summary = "根据声音ID查询声音统计信息")
    @GetMapping("/trackInfo/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }


    /**
     * 提供给小程序端，用户点击某个待购声音后，展示待购分集对象
     *
     * @param trackId
     * @return 【{"name":"本集",price:1, trackCount:1},{"name":"后10集",price:10, trackCount:10}】
     */
    @GuiGuLogin
    @Operation(summary = "获取当前用户分集购买集合")
    @GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> getUserWaitBuyTrackPayList(@PathVariable Long trackId) {
        Long userId = AuthContextHolder.getUserId();
        List<Map<String, Object>> list = trackInfoService.getUserWaitBuyTrackPayList(userId, trackId);
        return Result.ok(list);
    }


    /**
     * 渲染订单确认页面中待购声音（商品）集合
     *
     * @param trackId
     * @param trackCount
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "渲染订单确认页面中待购声音（商品）集合")
    @GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> getUserWaitPayTrackInfoList(@PathVariable Long trackId, @PathVariable Integer trackCount) {
        List<TrackInfo> userWaitPayTrackInfoList = trackInfoService.getUserWaitPayTrackInfoList(trackId, trackCount);
        return Result.ok(userWaitPayTrackInfoList);
    }

}

